/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */

package com.pig4cloud.pigx.strategy.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.pigx.common.core.util.R;
import com.pig4cloud.pigx.strategy.dto.StrategyStopOrderContractAddBatchDTO;
import com.pig4cloud.pigx.strategy.dto.StrategyStopOrderContractAddDTO;
import com.pig4cloud.pigx.strategy.dto.StrategyStopOrderContractCancelBatchDTO;
import com.pig4cloud.pigx.strategy.dto.StrategyStopOrderContractConditionDTO;
import com.pig4cloud.pigx.strategy.entity.StrategyStopOrderContract;
import com.pig4cloud.pigx.strategy.service.StrategyStopOrderContractService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 策略止盈止损-合约
 *
 * @since 2022-11-24 10:39:26
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/stop-order/contract")
@Api(value = "strategyStopOrderContract", tags = "策略止盈止损-合约管理")
public class StrategyStopOrderContractController {

    private final StrategyStopOrderContractService strategyStopOrderContractService;

    /**
     * 分页查询
     *
     * @param page      分页数据
     * @param condition 查询条件
     * @return {@link R< Page<  StrategyStopOrderContract >>}
     * @since 2023/2/13 09:44
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page")
    public R<Page<StrategyStopOrderContract>> page(Page<StrategyStopOrderContract> page, @Validated StrategyStopOrderContractConditionDTO condition) {
        return R.ok(strategyStopOrderContractService.page(page, condition));
    }

    /**
     * 新增止盈止损
     *
     * @param dto dto
     * @return {@link R< Object>}
     * @since 2023/2/8 15:15
     */
    @PostMapping
    public R<Object> add(@Validated @RequestBody StrategyStopOrderContractAddDTO dto) {
        strategyStopOrderContractService.add(dto);
        return R.ok();
    }

    /**
     * 批量新增止盈止损
     *
     * @param dto dto
     * @return {@link R< Object>}
     * @since 2023/2/8 15:15
     */
    @PostMapping("/batch")
    public R<Object> addBatch(@Validated @RequestBody StrategyStopOrderContractAddBatchDTO dto) {
        strategyStopOrderContractService.addBatch(dto);
        return R.ok();
    }

    /**
     * 撤销止盈止损
     *
     * @param dto dto
     * @return {@link R< Object>}
     * @since 2023/2/8 15:15
     */
    @PutMapping("/cancel/batch")
    public R<Object> cancelBatch(@Validated @RequestBody StrategyStopOrderContractCancelBatchDTO dto) {
        strategyStopOrderContractService.cancelBatch(dto);
        return R.ok();
    }

    /**
     * 撤销任务的所有止盈止损
     *
     * @param taskId 任务id
     * @return {@link R< Object>}
     * @since 2023/2/12 17:06
     */
    @PutMapping("/cancel/by-task/{taskId}")
    public R<Object> cancelByTask(@PathVariable("taskId") Long taskId) {
        strategyStopOrderContractService.cancelByTask(taskId);
        return R.ok();
    }

    /**
     * 批量撤销任务的所有止盈止损
     *
     * @param taskIdList 任务idList
     * @return {@link R< Object>}
     * @since 2023/2/12 17:06
     */
    @PutMapping("/cancel/by-task/batch")
    public R<Object> cancelByTaskBatch(@RequestBody List<Long> taskIdList) {
        strategyStopOrderContractService.cancelByTaskBatch(taskIdList);
        return R.ok();
    }

    /**
     * 一键撤销
     *
     * @param strategyId 策略id
     * @return {@link }
     * @since 2023/2/12 17:14
     */
    @PutMapping("/cancel/all/{strategyId}")
    public R<Object> cancelAll(@PathVariable("strategyId") Long strategyId) {
        strategyStopOrderContractService.cancelAll(strategyId);
        return R.ok();
    }
}
